<?php

class Api_model extends CI_Model{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    // get the day callenge
    public function get_the_callenge($day=1){
        $query =$this->db->get_where('callenges',array('call_time'=>date('Y-m-d',strtotime($day."day"))))->row_array();
        return $query;
    }
    public function partici_tomorrow_user($id){
        $this->db->join('users','sr_user=openid','left');
        $query = $this->db->limit(6,0)->order_by('sr_pay_time','asc')->get_where('sign_record',array('sr_callenge'=>$id));
        return $query->result_array();
    }
    public function get_user_champion($id,$u=1){
        $this->db->select("avatarUrl,openid,nickName,siteNum signNum,sr_acquire_money,from_unixtime(sr_sign_time,'%H:%i:%s') sr_sign_time");
        $this->db->join('users','sr_user=openid','left');
        if($u==1){
        $this->db->order_by('sr_acquire_money','desc');
        }else if($u==2){
            $this->db->order_by('sr_sign_time','asc');
        }else if($u==3){
            $this->db->order_by('siteNum','desc');
        }
        $query = $this->db->limit(1,0)->get_where('sign_record',array('sr_callenge'=>$id,'sr_status'=>1));
        return $query->row_array();
    }

    public function get_callenge_user_status($day,$openid){
        $this->db->join('sign_record','sr_callenge=call_id');

        $query =$this->db->get_where('callenges',array('sr_user'=>$openid,'call_time'=>date('Y-m-d',strtotime($day."day"))))->row_array();
        return $query;
    }
    //get by user info
    public function get_by_user($openid){
        return $this->db->get_where('users',array('openid'=>$openid))->row_array();
    }
    //get all user sign
    public function get_all_sign($openid){
        $this->db->select('sr_time,sr_status,sr_acquire_money,sr_pay');
        $this->db->order_by('sr_time','desc');
        return $this->db->get_where('sign_record',array('sr_user'=>$openid,'sr_is_close'=>1))->result_array();
    }

    /**
     * user insert sign_record
     * @param $data
     * @return mixed
     */
    public function create_sign_record($data){
        $this->db->insert('sign_record',$data);
        return $this->db->affected_rows();
    }
    public function sign_update_call_status($call_id,$openid){
        $this->db->where(array('call_id'=>$call_id))->set('call_num','call_num+1',FALSE)->update('callenges');
        $this->db->where(array('call_id'=>$call_id))->set('call_money','call_money+1',FALSE)->update('callenges');
        $this->db->where(array('openid'=>$openid))->set('inputMoney','inputMoney+1',FALSE)->update('users');
    }

//    user signuser_succeed_sign($openid,$id,$sr_formid)
    public function user_succeed_sign($openid,$id,$sr_formid){
        $this->db->where(array('call_id'=>$id))->set('call_sign_num','call_sign_num+1',FALSE)->update('callenges');
        $this->db->where(array('openid'=>$openid))->set('siteNum','siteNum+1',FALSE)->set('signNum','signNum+1',FALSE)->set('signTime',date("Y-m-d"))->update('users');
        $this->db->update('sign_record',array('sr_status'=>1,'sr_is_close'=>1,'sr_sign_time'=>time(),'sr_formid'=>$sr_formid),array('sr_user'=>$openid,'sr_callenge'=>$id));
    }
    public function user(){
        $this->db->where(array('call_id'=>1))->set('call_num','call_num+1',FALSE)->update('callenges');
    }

    public function get_by_system(){
        return $this->db->get_where('system',array('sys_id'=>1))->row_array();
    }

    public function sign_status_no($call,$openid){
        $this->db->update('sign_record',array('sr_is_close'=>1),array('sr_callenge'=>$call,'sr_user'=>$openid));
        return $this->db->affected_rows();
    }

    public function insert_feedback($data){
        $this->db->insert('feedbacks',$data);
        return $this->db->affected_rows();
    }

    public function insert_record($data){
        $this->db->insert('records',$data);
//        file_put_contents('/var/www/p021_checkin/server/uploads/text57.txt',$this->db->last_query());
        return $this->db->affected_rows();
    }

    //
    public function update_user_money($openid,$money){
        $this->db->where(array('openid'=>$openid))->set('money','money-'.$money,FALSE)->update('users');
        return $this->db->affected_rows();
    }
//插入订单
    public function insert_wd($da){
        $this->db->insert('Withdraw_deposit',$da);
        return $this->db->affected_rows();
    }
//修改订单状态
    public function update_wd($wd_no){
        $this->db->update('Withdraw_deposit',array('wd_status'=>1),array('wd_no'=>$wd_no));
        return $this->db->affected_rows();
    }
}